<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--类别数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="typeName"
            placeholder="请输类别名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="typeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--卷内目录数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="题名" prop="archiveId">
            <el-select v-model="queryParams.archiveId" filterable clearable placeholder="请选择">
              <el-option
                v-for="item in archiveList"
                :key="item.archiveId"
                :label="item.archiveName"
                :value="item.archiveId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="责任人" prop="personName">
            <el-input
              v-model="queryParams.personName"
              placeholder="请输入责任人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="身份证号" prop="idcard">
            <el-input
              v-model="queryParams.idcard"
              placeholder="请输入身份证号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="文件号" prop="fileNo">
            <el-input
              v-model="queryParams.fileNo"
              placeholder="请输入文件号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="daterangeCreateTime"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:person:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:person:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:person:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:person:export']"
            >导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handlePrint"
              v-hasPermi="['system:person:print']"
            >打印</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
          id="printJS-form"
          v-loading="loading"
          border
          :data="personList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="题名"
            align="center"
            prop="archive.archiveName"
            width="180"
            show-overflow-tooltip
          />
          <el-table-column label="责任人" align="center" prop="personName" />
          <el-table-column
            label="身份证号"
            align="center"
            prop="idcard"
            width="180"
            show-overflow-tooltip
          />
          <el-table-column label="页数" align="center" prop="pageCount" />
          <el-table-column label="文件号" align="center" prop="fileNo" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更新时间" align="center" prop="updateTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createBy" />
          <el-table-column label="更新人" align="center" prop="updateBy" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status == 0">在柜</el-tag>
              <el-tag type="info" v-if="scope.row.status == 1">已借出</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding"
            fixed="right"
            width="160"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleFind(scope.row)"
                v-hasPermi="['system:person:find']"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:person:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:person:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 查看卷内目录对话框 -->
    <el-dialog :title="title" :visible.sync="openFind" width="80%" append-to-body>
      <template>
        <el-row>
          <el-col :span="8">
            <span class="padding-r10">文件号:</span>
            <span>{{form.fileNo}}</span>
          </el-col>
          <el-col :span="8">
            <span class="padding-r10">身份证号:</span>
            <span>{{form.idcard}}</span>
          </el-col>
          <el-col :span="8">
            <span class="padding-r10">档案页数:</span>
            <span>{{form.pageCount}}</span>
          </el-col>
        </el-row>
        <el-divider content-position="center">数据挂接信息</el-divider>
        <el-table
          :data="recordCatalogueList"
          :row-class-name="rowRecordCatalogueIndex"
          ref="recordCatalogue"
        >
          <el-table-column label="目录名称" prop="catalogue" width="300">
            <template slot-scope="scope">
              <div style="font-family:'微软雅黑';font-size:20px">{{scope.row.catalogue}}</div>
            </template>
          </el-table-column>
          <el-table-column label="档案文件" prop="filePath">
            <template slot-scope="scope">
              <image-upload
                v-if="scope.row.imgLimit != 0"
                v-model="scope.row.imgPath"
                :limit="scope.row.imgLimit"
                :fileSize="scope.row.fileSize"
                :point="scope.row.point"
              />
              <el-divider
                content-position="center"
                v-if="scope.row.imgLimit != 0 && scope.row.fileLimit != 0"
              ></el-divider>
              <file-upload
                v-if="scope.row.fileLimit != 0"
                v-model="scope.row.filePath"
                :limit="scope.row.fileLimit"
                :fileSize="scope.row.fileSize"
                :point="scope.row.point"
              />
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改卷内目录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="题名" prop="archiveId">
              <el-select
                v-model="form.archiveId"
                filterable
                @change="archiveChange"
                clearable
                placeholder="请选择"
                :disabled="selectDisabled"
              >
                <el-option
                  v-for="item in archiveList"
                  :key="item.archiveId"
                  :label="item.archiveName"
                  :value="item.archiveId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="责任人" prop="personName">
              <el-input v-model="form.personName" placeholder="请输入责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证号" prop="idcard">
              <el-input v-model="form.idcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="页数" prop="pageCount">
              <el-input v-model="form.pageCount" placeholder="请输入页数" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="文件号" prop="fileNo">
              <el-input v-model="form.fileNo" placeholder="请输入文件号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">数据挂接信息</el-divider>
        <el-row :gutter="10" class="mb8" v-if="!showArcSet">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRecordCatalogue"
            >添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRecordCatalogue"
            >删除</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="recordCatalogueList"
          :row-class-name="rowRecordCatalogueIndex"
          @selection-change="handleRecordCatalogueSelectionChange"
          ref="recordCatalogue"
          v-if="showArcSet"
        >
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="目录名称" prop="catalogue" width="300">
            <template slot-scope="scope">
              <div style="font-family:'微软雅黑';font-size:20px">{{scope.row.catalogue}}</div>
            </template>
          </el-table-column>
          <el-table-column label="文件上传" prop="filePath">
            <template slot-scope="scope">
              <image-upload
                v-if="scope.row.imgLimit != 0"
                v-model="scope.row.imgPath"
                :limit="scope.row.imgLimit"
                :fileSize="scope.row.fileSize"
                :point="scope.row.point"
              />
              <el-divider
                content-position="center"
                v-if="scope.row.imgLimit != 0 && scope.row.fileLimit != 0"
              ></el-divider>
              <file-upload
                v-if="scope.row.fileLimit != 0"
                v-model="scope.row.filePath"
                :limit="scope.row.fileLimit"
                :fileSize="scope.row.fileSize"
                :point="scope.row.point"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-table
          :data="recordCatalogueList"
          :row-class-name="rowRecordCatalogueIndex"
          @selection-change="handleRecordCatalogueSelectionChange"
          ref="recordCatalogue"
          v-if="!showArcSet"
        >
          >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="目录名称" prop="catalogue" width="300">
            <template slot-scope="scope">
              <el-input
                type="textarea"
                style="font-family:'微软雅黑';font-size:20px"
                v-model="scope.row.catalogue"
                placeholder="请输入目录名称"
              />
            </template>
          </el-table-column>
          <el-table-column label="文件上传" prop="filePath">
            <template slot-scope="scope">
              <image-upload v-model="scope.row.imgPath" />
              <el-divider content-position="center"></el-divider>
              <file-upload v-model="scope.row.filePath" />
              <button type="button" @click="printImg(`${baseUrl}` + scope.row.filePath)">Print PDF</button>
              <div>
                <pdf ref="pdf" :src="`${baseUrl}` + scope.row.filePath"></pdf>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPerson,
  getPerson,
  delPerson,
  addPerson,
  updatePerson
} from "@/api/system/person";
import { listArchive } from "@/api/system/archive";
import { treeselect } from "@/api/system/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listArchiveset } from "@/api/system/archiveset";
import pdf from "vue-pdf";
import print from "print-js";

export default {
  name: "Person",
  components: {
    pdf
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      url:
        "http://localhost/dev-api/profile/upload/2022/%E7%BB%BC%E5%90%88%E7%9B%AE%E5%BD%95/20220226181620-%E7%BB%87%E9%87%91.pdf",
      // 切换是否定制上传配置项
      showArcSet: true,
      // 类别名称
      typeName: undefined,
      // 档案分类表格数据
      typeOptions: undefined,
      // 查询可用状态类别参数
      // 树形菜单参数
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 案卷表格数据
      archiveList: [],
      // 下拉框禁用状态
      selectDisabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRecordCatalogue: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卷内目录表格数据
      personList: [],
      // 数据挂接表格数据
      recordCatalogueList: [],
      // 弹出层标题
      title: "",
      // 是否显示删除修改弹出层
      open: false,
      // 是否显示查询弹出层
      openFind: false,
      // 文件后缀名时间范围
      daterangeCreateTime: [],
      // 卷内目录查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personName: null,
        idcard: null,
        archiveId: null,
        fileNo: null,
        createTime: null,
        status: null
      },

      // 表单参数
      form: {
        personId: null,
        personName: null,
        idcard: null,
        archiveId: null,
        pageCount: null,
        fileNo: null,
        archive: { archiveName: null }
      },
      // 表单校验
      rules: {
        archiveId: [
          { required: true, message: "题名不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getListArchive();
  },
  methods: {
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeId = data.id;
      this.handleQuery();
    },
    /** 获取树形菜单结构数据 */
    getTreeselect() {
      treeselect(this.queryType).then(response => {
        this.typeOptions = response.data;
      });
    },
    // 获取案卷目录列表
    getListArchive() {
      listArchive().then(response => {
        this.archiveList = response.rows;
      });
    },
    /** 查询卷内目录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params[
          "beginCreateTime"
        ] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows;
        console.log(this.personList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openFind = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        personId: null,
        personName: null,
        idcard: null,
        archiveId: null,
        pageCount: null,
        fileNo: null
      };
      this.recordCatalogueList = [];
      this.resetForm("form");
      this.showArcSet = true;
    },
    // 下拉选中题名，获取目录模板
    archiveChange(archiveId) {
      listArchiveset({ archiveId: archiveId }).then(response => {
        this.recordCatalogueList = response.rows;
        if (this.recordCatalogueList.length == 0) {
          // 切换是否定制上传配置项
          this.showArcSet = false;
        } else {
          this.showArcSet = true;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleFind(row) {
      const personId = row.personId || this.ids;
      getPerson(personId).then(response => {
        this.form = response.data; // 按personId获取卷内目录初始数据
        this.recordCatalogueList = response.data.recordCatalogueList; //取出目录初始数据
        this.openFind = true;
        this.title =
          this.form.personName + "的" + this.form.archive.archiveName;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.selectDisabled = false;
      this.reset();
      this.open = true;
      this.title = "添加卷内目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.selectDisabled = true;
      const personId = row.personId || this.ids;
      getPerson(personId).then(response => {
        this.form = response.data; // 按personId获取卷内目录初始数据
        this.recordCatalogueList = response.data.recordCatalogueList; //取出目录初始数据
        if (this.recordCatalogueList[0].archivesetId == null) {
          this.showArcSet = false;
        } else {
          this.showArcSet = true;
        }
        this.open = true;
        this.title = "修改卷内目录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.recordCatalogueList = this.recordCatalogueList;
          if (this.form.personId != null) {
            updatePerson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const personIds = row.personId || this.ids;
      this.$modal
        .confirm('是否确认删除卷内目录编号为"' + personIds + '"的数据项？')
        .then(function() {
          return delPerson(personIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 数据挂接序号 */
    rowRecordCatalogueIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 数据挂接添加按钮操作 */
    handleAddRecordCatalogue() {
      let obj = {};
      obj.catalogue = "";
      obj.filePath = "";
      this.recordCatalogueList.push(obj);
    },
    /** 数据挂接删除按钮操作 */
    handleDeleteRecordCatalogue() {
      if (this.checkedRecordCatalogue.length == 0) {
        this.$modal.msgError("请先选择要删除的数据挂接数据");
      } else {
        const recordCatalogueList = this.recordCatalogueList;
        const checkedRecordCatalogue = this.checkedRecordCatalogue;
        this.recordCatalogueList = recordCatalogueList.filter(function(item) {
          return checkedRecordCatalogue.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleRecordCatalogueSelectionChange(selection) {
      this.checkedRecordCatalogue = selection.map(item => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/person/export",
        {
          ...this.queryParams
        },
        `person_${new Date().getTime()}.xlsx`
      );
    },
    handlePrint() {
      console.log("打印");
      print({
        printable: "printJS-form",
        type: "html",
        header: "采购单"
      });
    },
    printImg(value) {
      console.log(value)
      print(value)
    }
  }
};
</script>
<style>
.padding-r10 {
  padding-right: 10px;
}
</style>
